Left-Shift (<<)
The left shift operator is denoted by the double left arrow key (<<). The general syntax for left shift is shift-expression << k. The left-shift operator causes the bits in shift expression to be shifted to the left by the number of positions specified by k. The bit positions that the shift operation has vacated are zero-filled.
Note: Every time we shift a number towards the left by 1 bit it multiply that number by 2.
Example:
Input: Left shift of 5 by 1.
Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 1)
Output: 10
Explanation: All bit of 5 will be shifted by 1 to left side and this result in 010102, Which is equivalent to 10Input: Left shift of 5 by 2.
Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 2)Output: 20
Explanation: All bit of 5 will be shifted by 1 to left side and this result in 101002, Which is equivalent to 20Input: Left shift of 5 by 3.
Binary representation of 5 = 00101 and Left shift of 001012 by 1 (i.e, 00101 << 3)Output: 40
Explanation: All bit of 5 will be shifted by 1 to left side and this result in 010002, Which is equivalent to 40
Implementation of Left shift operator:
#include <bits/stdc++.h>
using namespace std;
int main()
{
unsigned int num1 = 1024;
bitset<32> bt1(num1);
cout << bt1 << endl;
unsigned int num2 = num1 << 1;
bitset<32> bt2(num2);
cout << bt2 << endl;
unsigned int num3 = num1 << 2;
bitset<16> bitset13{ num3 };
cout << bitset13 << endl;
}
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int num1 = 1024;
String bt1 = Integer.toBinaryString(num1);
bt1 = String.format("%32s", bt1).replace(' ', '0');
System.out.println(bt1);
int num2 = num1 << 1;
String bt2 = Integer.toBinaryString(num2);
bt2 = String.format("%32s", bt2).replace(' ', '0');
System.out.println(bt2);
int num3 = num1 << 2;
String bitset13 = Integer.toBinaryString(num3);
bitset13 = String.format("%16s", bitset13)
.replace(' ', '0');
System.out.println(bitset13);
}
}
// This code is contributed by akashish__
# Python code for the above approach
num1 = 1024
bt1 = bin(num1)[2:].zfill(32)
print(bt1)
num2 = num1 << 1
bt2 = bin(num2)[2:].zfill(32)
print(bt2)
num3 = num1 << 2
bitset13 = bin(num3)[2:].zfill(16)
print(bitset13)
# This code is contributed by Prince Kumar
using System;
class GFG {
public static void Main(string[] args)
{
int num1 = 1024;
string bt1 = Convert.ToString(num1, 2);
bt1 = bt1.PadLeft(32, '0');
Console.WriteLine(bt1);
int num2 = num1 << 1;
string bt2 = Convert.ToString(num2, 2);
bt2 = bt2.PadLeft(32, '0');
Console.WriteLine(bt2);
int num3 = num1 << 2;
string bitset13 = Convert.ToString(num3, 2);
bitset13 = bitset13.PadLeft(16, '0');
Console.WriteLine(bitset13);
}
}
// This code is contributed by akashish__
// JavaScript code for the above approach
let num1 = 1024;
let bt1 = num1.toString(2).padStart(32, '0');
console.log(bt1);
let num2 = num1 << 1;
let bt2 = num2.toString(2).padStart(32, '0');
console.log(bt2);
let num3 = num1 << 2;
let bitset13 = num3.toString(2).padStart(16, '0');
console.log(bitset13);
Output
00000000000000000000010000000000 00000000000000000000100000000000 0001000000000000
Time Complexity: O(1)
Auxiliary Space: O(1)
Introduction to Bitwise Algorithms – Data Structures and Algorithms Tutorial
Bit stands for binary digit. A bit is the basic unit of information and can only have one of two possible values that is 0 or 1. In our world, we usually with numbers using the decimal base. In other words. we use the digit 0 to 9 However, there are other number representations that can be quite useful such as the binary number systems.
Unlike humans, computers have no concepts of words and numbers. They receive data encoded at the lowest level as a series of zeros and ones (0 and 1). These are called bits, and they are the basis for all the commands they receive. We’ll begin by learning about bits and then explore a few algorithms for manipulating bits. We’ll then explore a few algorithms for manipulating bits. The tutorial is meant to be an introduction to bit algorithms for programmers.
Table of Content
- What is Bitwise Algorithms?
- Bitwise Operators / Basics of Bit manipulation
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Bitwise NOT Operator (!~)
- Left-Shift (<<)
- Right-Shift (>>)
- Application of Bit Operators
- Important Practice Problems on Bitwise Algorithm
Contact Us